 //-------------配置开始----
 var package_name="com.sjf.xlqsg.app.huawei"
 var app_name="绚丽切水果"
 
 var app_id="6917567106234489931";
 var client_id="6917567106234489931";
 
 var companename="深圳市时间坊贸易有限公司";
 /**软著信息如果游戏里没有健康忠告软著信息，可以在这里设置，不需要则填空白**/
 var jkzg="登记号:2022SR1538377 \\n著作权人：深圳阳阳无限科技有限公";
 //-----
 var url="https://www.szsjmyi.com/aspcms/about/about-492.html";
 
 var insertId="";
 var rewardId="r0giwku81f";
 var splashId="t7r5dgvme1";
 var bannerId="";
 /**0顶部，1底部**/
 var banner_type=1;
 /**=1竖屏，2横屏**/
 var  orientation=2;
 
 //是否使用本地隐私政策
 var localUrl=false;
 
 //是否显示同意隐私政策勾选框
 var showCheck=true;
 //切换测试id
 var testId=false;
 
 
 //--------------配置结束-------------------
 
  //--------------配置结束-------------------
 
  const fs = require('fs');
  const path = require('path');
  function printLog(str) {
    console.log(str)
  }
  function replace(totalStr, bjStr, bfStr, afStr, rpconent) {
 
    let n1 = totalStr.indexOf(bjStr);
 
    let beforeTotalStr = totalStr.substring(0, n1);
    let afterTotalStr = totalStr.substring(n1, totalStr.length);// 去标记字符串后面字符
 
    printLog(afterTotalStr);
    // printLog(bfStr);
    let bfn = afterTotalStr.indexOf(bfStr);
    // printLog("bfn" + bfn);
    let bfnStr = afterTotalStr.substring(0, bfn + bfStr.length);
 
    // printLog(bfnStr);
    let afbfstr = afterTotalStr.substring(bfn + bfStr.length, afterTotalStr.length);
    let afbfn = afbfstr.indexOf(afStr);
 
    let rpc = afbfstr.substring(0, afbfn);
    // System.out.println(rpc);
 
    rpc = rpconent;
 
    let afnstr = afbfstr.substring(afbfn, afbfstr.length);
 
    // System.out.println(afnstr);
 
    let str = beforeTotalStr + bfnStr + rpc + afnstr;
    // printLog(str);
    return str;
 
  }
 
  function changefile(path, bjStr, bfStr, afStr, rpconent) {
    //'AppScope/app.json5'
 
    return new Promise((resolve, reject) => {
      fs.readFile(path, 'utf8', (err, data) => {
        if (err) throw err;
        // console.log(data);
        data = replace(data, bjStr, bfStr, afStr, rpconent)
 
        fs.writeFile(path, data, (err) => {
          if (err) throw err;
          console.log(path + ' File has been saved!');
 
          setTimeout(() => {
            resolve(data);
          }, 10);
 
        });
      });
 
    });
 
 
  }
 
 
  function changefile1(path, old, rpconent) {
    //'AppScope/app.json5'
 
    return new Promise((resolve, reject) => {
      fs.readFile(path, 'utf8', (err, data) => {
        if (err) throw err;
        // console.log(data);
        data = data.replace(old, rpconent)
 
        fs.writeFile(path, data, (err) => {
          if (err) throw err;
          console.log(path + ' File has been saved!');
 
          setTimeout(() => {
            resolve(data);
          }, 10);
 
        });
      });
 
    });
 
 
  }
 
  function getCurrentDateTime() {
    const date = new Date(); // 获取当前日期时间对象
 
    // 提取年份（四位数字）
    const year1 = String(date.getFullYear());
    const year = year1.substring(year1.length - 2, year1.length);
    // 提取月份（0-11 → 转为 1-12，并补零）
    const month = String(date.getMonth() + 1).padStart(2, '0');
 
    // 提取日期（1-31，补零）
    const day = String(date.getDate()).padStart(2, '0');
 
    // 提取小时（0-23，补零）
    const hours = String(date.getHours()).padStart(2, '0');
 
    // 拼接为 yyyymmddhhmm 格式
    return `${year}${month}${day}${hours}`;
  }
 
 
  /**
   * 判断当前目录下是否存在指定文件夹
   * @param {string} folderName 文件夹名称（如 'libSysCapabilities'）
   * @returns {boolean} 是否存在
   */
  function isFolderExists(folderName) {
    // 构造目标文件夹的绝对路径（当前目录为脚本执行目录）
    const currentDir = process.cwd(); // 或 __dirname（模块所在目录）
    const targetPath = path.join(currentDir, folderName);
 
    try {
      // 获取路径状态（若不存在会抛出错误）
      const stats = fs.statSync(targetPath);
      // 检查是否为目录（避免误判为同名文件）
      return stats.isDirectory();
    } catch (err) {
      // 错误码 ENOENT 表示路径不存在
      if (err.code === 'ENOENT') {
        return false;
      }
      // 其他错误（如权限不足）抛出异常
      throw err;
    }
  }
 
  async function init() {
    console.log(getCurrentDateTime());
    const versioncode = getCurrentDateTime();
 
 
 
    await changefile('AppScope/app.json5', "versionCode", ": ", ",", versioncode)
    await changefile('AppScope/app.json5', "versionName", ": \"", "\"", "1.0." + versioncode)
    await changefile('AppScope/app.json5', "bundleName", ": \"", "\"", package_name)
    await changefile('AppScope/resources/base/element/string.json', "app_name", ": \"", "\"", app_name)
    await changefile('Module_SDK/src/main/resources/rawfile/sdkAssets/assets/test.html', "欢迎您选择", "由", "（", companename)
    await changefile('Module_SDK/src/main/resources/rawfile/sdkAssets/assets/useragreement20210224.html', "本协议是用户", "与", "（", companename)
    await changefile('Module_SDK/src/main/ets/GloConfig.ets', "jkzg:string", "=\"", "\";", jkzg)
    await changefile('Module_SDK/src/main/module.json5', "client_id", ": \"", "\"", client_id)
    await changefile('Module_SDK/src/main/module.json5', "app_id", ": \"", "\"", app_id)
    await changefile('Module_SDK/src/main/ets/GloConfig.ets', "url:string", "=\"", "\";", url)
    await changefile('Module_SDK/src/main/ets/GloConfig.ets', "insertId:string", "=\"", "\";", insertId)
    await changefile('Module_SDK/src/main/ets/GloConfig.ets', "rewardId:string", "=\"", "\";", rewardId)
    await changefile('Module_SDK/src/main/ets/GloConfig.ets', "splashId:string", "=\"", "\";", splashId)
    await changefile('Module_SDK/src/main/ets/GloConfig.ets', "bannerId:string", "=\"", "\";", bannerId)
 
    await changefile('Module_SDK/src/main/ets/GloConfig.ets', "banner_type", "=", ";", banner_type)
 
    await changefile('Module_SDK/src/main/ets/GloConfig.ets', "orientation", "=", ";", orientation)
    await changefile('Module_SDK/src/main/ets/GloConfig.ets', "localUrl:boolean", "=", ";", localUrl)
    await changefile('Module_SDK/src/main/ets/GloConfig.ets', "showCheck:boolean", "=", ";", showCheck)
    await changefile('Module_SDK/src/main/ets/GloConfig.ets', "testId:boolean", "=", ";", testId)
    await changefile1("entry/build-profile.json5", '"armeabi-v7a",', "");
 
 
    await changefile("entry/src/main/module.json5", 'orientation', ":", ",", orientation == 1 ? "\"portrait\"" : "\"landscape\"");
    if (isFolderExists('libSysCapabilities')) {
      // await changefile("entry/src/main/module.json5", 'EntryAbility,', "\"orientation\":", ",", orientation == 1 ? "\"portrait\"" : "\"landscape\"");
 
      await changefile('Module_SDK/src/main/ets/GloConfig.ets', "mainGamePage:string", "=\"", "\";", "pages/Index")
    } else
      if (isFolderExists('tuanjieLib')) {
        // await changefile("entry/src/main/module.json5", 'EntryAbility,', "\"orientation\":", ",", orientation == 1 ? "\"portrait\"" : "\"landscape\"");
        await changefile('Module_SDK/src/main/ets/GloConfig.ets', "mainGamePage:string", "=\"", "\";", "pages/Index")
      } else {
 
      // await changefile("entry/src/main/module.json5", 'orientation', ":", ",", orientation == 1 ? "\"portrait\"" : "\"landscape\"");
    }
 
 
    //changefile('Module_SDK/src/main/module.json5',"client_id",": \"","\"","33333");
  }
  init();